package com.maila.xiaoaobusiness.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import com.maila.xiaoaobusiness.R;
import com.maila.xiaoaobusiness.entry.ShopComment;
import com.maila.xiaoaobusiness.utils.Constants;
import com.maila.xiaoaobusiness.widget.CircleImageView;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 评论adapter
 * Created by Administrator on 2017/9/26.
 */

public class CommentAdapter extends BaseAdapter {
    Context context;
    List<ShopComment.DataBean> mdata;
    public CommentAdapter(Context context) {
        this.context = context;
        mdata=new ArrayList<>();
    }

    public List<ShopComment.DataBean> getMdata() {
        return mdata;
    }

    public void setMdata(List<ShopComment.DataBean> mdata) {
        this.mdata = mdata;
        notifyDataSetChanged();
    }
    public void addMdata(List<ShopComment.DataBean> mdata) {
        this.mdata.addAll(mdata) ;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mdata.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_comment, viewGroup, false);
            holder = new ViewHolder(view);
            view.setTag(holder);
            AutoUtils.autoSize(view);
        } else {
            holder = (ViewHolder) view.getTag();
        }
       Glide.with(context).load(Uri.parse(Constants.URL_ROOT_XIAOAO+mdata.get(i).getLogo())).into(holder.ivHeader);
        holder.tvComShangpin.setText(mdata.get(i).getPoint());
        holder.tvComPeisong.setText(mdata.get(i).getPs_point());
        if(NullPointUtils.checknull(mdata.get(i).getImgs())){
            holder.ivPhoto1.setVisibility(View.GONE);
            holder.ivPhoto2.setVisibility(View.GONE);
            holder.ivPhoto3.setVisibility(View.GONE);
        }else {
            String[] imgs=mdata.get(i).getImgs().split(",");
            holder.ivPhoto1.setVisibility(View.GONE);
            holder.ivPhoto2.setVisibility(View.GONE);
            holder.ivPhoto3.setVisibility(View.GONE);
            switch (imgs.length){
                case 1:
                    holder.ivPhoto1.setVisibility(View.VISIBLE);
                    Glide.with(context).load(Uri.parse(Constants.URL_ROOT_XIAOAO+imgs[0])).into(holder.ivPhoto1);
                    break;
                case 2:
                    holder.ivPhoto2.setVisibility(View.VISIBLE);
                    holder.ivPhoto1.setVisibility(View.VISIBLE);
                    Glide.with(context).load(Uri.parse(Constants.URL_ROOT_XIAOAO+imgs[0])).into(holder.ivPhoto1);
                    Glide.with(context).load(Uri.parse(Constants.URL_ROOT_XIAOAO+imgs[1])).into(holder.ivPhoto2);
                    break;
                case 3:
                    holder.ivPhoto1.setVisibility(View.VISIBLE);
                    holder.ivPhoto2.setVisibility(View.VISIBLE);
                    holder.ivPhoto3.setVisibility(View.VISIBLE);
                    Glide.with(context).load(Uri.parse(Constants.URL_ROOT_XIAOAO+imgs[0])).into(holder.ivPhoto1);
                    Glide.with(context).load(Uri.parse(Constants.URL_ROOT_XIAOAO+imgs[1])).into(holder.ivPhoto2);
                    Glide.with(context).load(Uri.parse(Constants.URL_ROOT_XIAOAO+imgs[2])).into(holder.ivPhoto3);
                    break;
            }
        }
        holder.tvFoodinfo.setText(mdata.get(i).getGoodsname());
        holder.tvTime.setText(mdata.get(i).getAddtime());
        holder.tvName.setText(mdata.get(i).getUsername());
        holder.tvReviews.setText(mdata.get(i).getContent());
        return view;
    }


    static class ViewHolder {
        @BindView(R.id.iv_header)
        CircleImageView ivHeader;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.user_info)
        AutoRelativeLayout userInfo;
        @BindView(R.id.iv_photo_1)
        ImageView ivPhoto1;
        @BindView(R.id.iv_photo_2)
        ImageView ivPhoto2;
        @BindView(R.id.iv_photo_3)
        ImageView ivPhoto3;
        @BindView(R.id.all_photo)
        AutoLinearLayout allPhoto;
        @BindView(R.id.tv_com_peisong)
        TextView tvComPeisong;
        @BindView(R.id.tv_com_shangpin)
        TextView tvComShangpin;
        @BindView(R.id.tv_reviews)
        TextView tvReviews;
        @BindView(R.id.tv_foodinfo)
        TextView tvFoodinfo;
        @BindView(R.id.ll_content)
        AutoLinearLayout llContent;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
